/**
 * Created by TomSean on 2017/10/23.
 */
var LicenseCorrectRecordManager = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
LicenseCorrectRecordManager.prototype = {
    init: function () {
        var bindFieldArray = this.sysUtils.getBindFieldArray(this.$parent.find(".search-panel"));
        var bindFunctionArray = [
            {fName: "find", fs: this.findData},
            {fName: "addData", fs: this.addData},
            {fName: "updateData", fs: this.updateData},
            {fName: "reset", fs: this.reset}];
        this.bindBaseModels = new bindBaseModel(null, bindFieldArray, bindFunctionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
        this.createTable();
        this.findData();
    },
    createTable: function () {
        var columns = [
            {
                width: 30,
                headerTemplate: '<input type="checkbox" gridId="license_correct_record_list" onclick="gridCheckAll(this)" />',
                template: '<input name="id" type="checkbox" value="${id}"/>'
            },
            {
                title: "纠正前车牌",
                width: 200,
                field: "beforechangeVehno"
            },
            {
                title: "纠正后车牌",
                width: 200,
                field: "afterchangeVehno"
            },
            {
                title: "纠正人姓名",
                width: 155,
                field: "changeName"
            },
            {
                title: "纠正时间",
                width: 185,
                field: "changeTime"
            }
        ];
        var $this = this;
        this.sysUtils.createGird($this.$parent.find("#license_correct_record_list"), {
            selectable: "single",
            autoSync: true,
            pageable: {input: true, numeric: false},
            columns: columns,
            dataBound: function () {
                $this.$parent.find("#license_correct_record_list").find("tr:gt(0)").unbind("dblclick");
                $this.$parent.find("#license_correct_record_list").find("tr:gt(0)").dblclick(function (e) {
                    var grid = $this.$parent.find("#license_correct_record_list").data("kendoGrid");
                    var dataItem = grid.dataItem(grid.select());
                    //打开查看窗口
                    $this.openResourcesWindow(dataItem["id"], false);
                });
            }
        });
    },
    reset : function (e) {
        var $this = $(e.target).data('that');
        $this.sysUtils.setBindFieldNull($this.bindBaseModels.bindFieldArray);
        $this.findData();
    },
    findData : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        var data = {"params" : JSON.stringify($this.sysUtils.getBindDocJson($this.bindBaseModels.bindFieldArray))};
        var list = $this.$parent.find('#license_correct_record_list').data('kendoGrid');
        var dataSource = $this.sysUtils.getDataSource({
            url : baseApiPath + 'license/correct/record/findAll',
            data : data,
            dataType : 'odata'
        });
        list.setDataSource(dataSource);
    },
    addData : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        $this.openResourcesWindow(undefined, true)
    },
    updateData : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        var grid = $this.$parent.find("#license_correct_record_list").data("kendoGrid");
        var dataItem = grid.dataItem(grid.select());
        //打开查看窗口
        $this.openResourcesWindow(dataItem["id"], true);
    },
    deleteData : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data("that");
        }
        var ids = "";
        _this.$parent.find("input[name=\"id\"]:checked").each(function(inx, itm) {
            var id = $(itm).val();
            ids += id + ",";
        });
        if (ids == "") {
            notification({ title:"操作失败", message:"请选择至少一条需要删除的记录!!"},'info');
            return;
        }
        notification({title:"确认信息", message:"确定需要删除选中的信息？"}, 'confirm', {
            callback : function(){
                JsonpAjax(baseApiPath + "license/correct/record/delete?ids="+ids,"","json",function(res){
                    _this.findData();
                });
            }
        });
    },
    openResourcesWindow : function (id, edit) {
        var $this = this;
        $this.edit = isEmpty(edit) ? true : edit;
        if (!isEmpty(id)) {

            JsonpAjax(baseApiPath + 'license/correct/record/' + id, undefined, 'json', function(res) {
                $this.cacheRes = res;
                $this.remarkWindows = $this.sysUtils.openWindow($this.remarkWindows,{
                    title:'纠正车牌',
                    height:"215px",
                    width:"680px",
                    resizable:false,
                    content:"views/parkLotCenter/parkingLotInfo/license_correct_record_edit.html"
                }, true);
            }, false);
        } else {
            $this.remarkWindows = $this.sysUtils.openWindow($this.remarkWindows,{
                title:'纠正车牌',
                height:"215px",
                width:"680px",
                resizable:false,
                content:"views/parkLotCenter/parkingLotInfo/license_correct_record_edit.html"
            }, true);
        }
    }
}

var LicenseCorrectRecordEdit = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
LicenseCorrectRecordEdit.prototype = {
    init: function () {
        var bindFieldArray = this.sysUtils.getBindFieldArray(this.$parent);
        var bindFunctionArray = [{fName: "save", fs: this.save}, {fName:"cancel", fs:closeWindow}];
        this.bindBaseModels = new bindBaseModel(null, bindFieldArray, bindFunctionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
        this.deployData();
    },
    deployData : function () {
        var res = licenseCorrectManager.cacheRes;
        if (!isEmpty(res)) {
            this.sysUtils.setBindFieldValue(res);
            licenseCorrectManager.cacheRes = undefined;

        }
        if (licenseCorrectManager.edit == true) {
            this.$parent.find('#save').show();
            this.$parent.find('input[data-bind]').removeAttr('disabled');
            this.$parent.find('textarea[data-bind]').removeAttr('disabled');
        }
        licenseCorrectManager.edit = false;
    },
    save : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        var params = $this.sysUtils.getBindDocJson();
        var dataT = {jsonParam : params}
        if (validityForm($this.$parent)) {
            JsonpAjax(baseApiPath + "license/correct/record/save", JSON.stringify(dataT), "json", function(res) {
                licenseCorrectManager.findData();
                $('#' + licenseCorrectManager.remarkWindows).data('kendoWindow').close();
            });
        }
    }
}